package com.xju.admin.service.Impl;

import com.xju.admin.entity.*;
import com.xju.admin.mapper.QuestionMapper;
import com.xju.admin.service.DictionaryService;
import com.xju.admin.service.QuestionService;
import com.xju.admin.service.SectionService;
import com.xju.admin.service.UserService;
import com.xju.admin.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private UserService userService;

    @Override
    public QuestionResult findAll() {
        List<Question> questionList;
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试题状态","可用");
        QuestionExample questionExample=new QuestionExample();
        questionExample.createCriteria().andQuesStatusEqualTo(dictionary.getDicId());
        questionList=questionMapper.selectByExampleWithBLOBs(questionExample);
        List<QuestionShow> questionShowList=new ArrayList<>();
        for (Question question:questionList){
            QuestionShow questionShow=new QuestionShow(question);
            Dictionary questionstatus=dictionaryService.findById(question.getQuesStatus());
            questionShow.setNewStatus(questionstatus.getDicValue());
            Section section=sectionService.findById(question.getQuesSection());
            questionShow.setNewSection(section.getSecName());
            User create=userService.findById(question.getQuesCreator());
            questionShow.setNewCreator(create.getUserName());
            if (question.getQuesLastModifyUser()!=null){
                User modify=userService.findById(question.getQuesLastModifyUser());
                questionShow.setNewModifor(modify.getUserName());
            }
            String createTime= DateUtil.dateFormat(question.getQuesCreateTime());
            questionShow.setNewCreateTime(createTime);
            if (question.getQuesLastModifyTime()!=null){
                String modifyTime=DateUtil.dateFormat(question.getQuesLastModifyTime());
                questionShow.setNewModifyTime(modifyTime);
            }
            Dictionary type=dictionaryService.findById(question.getQuesType());
            questionShow.setNewType(type.getDicValue());
            questionShowList.add(questionShow);
        }
        List<Dictionary> statusList=dictionaryService.findByType("试题状态");
        QuestionResult questionResult=new QuestionResult(questionShowList,statusList);
        return questionResult;
    }

    @Override
    public void modify(String id, String content, String answer, String describe, String status) {
        Question question=questionMapper.selectByPrimaryKey(id);
        question.setQuesContent(content);
        question.setQuesAnswer(answer);
        question.setQuesDescribe(describe);
        question.setQuesStatus(status);
        questionMapper.updateByPrimaryKeyWithBLOBs(question);
    }

    @Override
    public void delete(String id) {
        questionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public QuestionResult findSelected() {
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试题状态","待审核");
        List<Question> questionList;
        QuestionExample questionExample=new QuestionExample();
        questionExample.createCriteria().andQuesStatusEqualTo(dictionary.getDicId());
        questionList=questionMapper.selectByExampleWithBLOBs(questionExample);
        List<QuestionShow> questionShowList=new ArrayList<>();
        for (Question question:questionList){
            QuestionShow questionShow=new QuestionShow(question);
            Dictionary questionstatus=dictionaryService.findById(question.getQuesStatus());
            questionShow.setNewStatus(questionstatus.getDicValue());
            Section section=sectionService.findById(question.getQuesSection());
            questionShow.setNewSection(section.getSecName());
            User create=userService.findById(question.getQuesCreator());
            questionShow.setNewCreator(create.getUserName());
            if (question.getQuesLastModifyUser()!=null){
                User modify=userService.findById(question.getQuesLastModifyUser());
                questionShow.setNewModifor(modify.getUserName());
            }
            String createTime= DateUtil.dateFormat(question.getQuesCreateTime());
            questionShow.setNewCreateTime(createTime);
            if (question.getQuesLastModifyTime()!=null){
                String modifyTime=DateUtil.dateFormat(question.getQuesLastModifyTime());
                questionShow.setNewModifyTime(modifyTime);
            }
            Dictionary type=dictionaryService.findById(question.getQuesType());
            questionShow.setNewType(type.getDicValue());
            questionShowList.add(questionShow);
        }
        List<Dictionary> statusList=dictionaryService.findByType("试题状态");
        QuestionResult questionResult=new QuestionResult(questionShowList,statusList);
        return questionResult;
    }

    @Override
    public Question findById(String id) {
        QuestionExample questionExample=new QuestionExample();
        questionExample.createCriteria().andQuesIdEqualTo(id);
        Question question=questionMapper.selectByExampleWithBLOBs(questionExample).get(0);
        return question;
    }

    @Override
    public void pass(String id) {
        Question question=findById(id);
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试卷状态","可用");
        question.setQuesStatus(dictionary.getDicId());
        questionMapper.updateByPrimaryKeyWithBLOBs(question);
    }
}
